package org.example.xiaomi.controller;


import com.wf.captcha.ArithmeticCaptcha;

import org.example.xiaomi.util.HttpResponseUtil;
import org.example.xiaomi.util.RedisUtil;
import org.example.xiaomi.vo.ResultVO;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@WebServlet("/captcha")
public class CaptchaController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        String verCode = captcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        RedisUtil.getJedis().setex(key, 30000, verCode);
        // 将key和base64返回给前端
        Map<String, Object> map = new HashMap<>();
        map.put("image", captcha.toBase64());
        map.put("key", key);
        HttpResponseUtil.response(resp,ResultVO.success(map));
    }
}
